package com.hhxy.practice;

import java.util.Random;
//生成验证码
public class GenerateCaptcha {
    public static void main(String[] args) {
        char[] captcha = new char[4];
        generateCaptcha(captcha);
        System.out.print("随机生成的验证码为：");
        for (int i = 0; i < captcha.length; i++) {
            System.out.print(captcha[i]);
        }
    }
    //生成验证码
    private static void generateCaptcha(char[] captcha) {
        Random r = new Random();
        int type;
        for (int i = 0; i < 4; i++) {
            type = r.nextInt(3);
            switch (type) {
                //大写字母的ASCII码值：65~90
                case 0:
                    captcha[i] = (char)(r.nextInt(26) + 65);
                    break;
                //小写字母的ASCII码值：97~122
                case 1:
                    captcha[i] = (char)(r.nextInt(26) + 97);
                    break;
                //数字的ASCII码值：48~57
                case 2:
                    captcha[i] = (char)(r.nextInt(10) + 48);
                    break;
            }
        }
    }

}
